﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MX.Core.Http.WebAction
{
    abstract public class BaseRunner<T> : IRunner<T>
    {
        private OnComplatedFun<T> onComplatedFun;
        private Thread actionThread;
        public void SetOnComplated(OnComplatedFun<T> fun)
        {
            onComplatedFun = fun;
        }



        public void Start()
        {
            actionThread = new Thread(() =>
                {
                    DoRun();
                    onComplatedFun(GetComplatedResult());

                });
            actionThread.Start();
        }

        protected abstract T GetComplatedResult();


        abstract protected void DoRun();

        protected virtual void AfterStop()
        {
        }

        public void Stop()
        {
            AfterStop();
            if (actionThread != null)
            {
                try
                {
                  
                    actionThread.Abort();
                }
                catch (Exception)
                {
                }
            }
        }


        public void Suspend()
        {
            actionThread.Suspend();
        }

        public void Resume()
        {
            actionThread.Resume();
        } 

        public abstract void Init(params object[] pars);

    }
}
